﻿#pragma
#ifndef BINARYFILE_WRITER_H
#define BINARYFILE_WRITER_H
#include <iostream>
#include <fstream>
#include <string>
#include <QDebug>

/// <summary>
/// 二进制文件写入工具
/// </summary>
class BinaryFileWriter {

private:
	//
	std::ofstream out;

public:
	bool Init(std::string filename)
	{
		Close();
		out.open(filename, std::ios::binary);
		if (!out.is_open())
		{
			qDebug() << "无法打开写入文件";
			return false;
		}
		return true;
	}

	bool Close()
	{
		if (out) {
			out.flush();
			out.close();
		}
		return true;
	}
	/// <summary>
	/// 向文件中写入数据
	/// </summary>
	/// <param name="data">要写入的数据</param>
	/// <param name="size">数据的长度（单位是Byte）</param>
	/// <returns></returns>
	bool Write(char* data, int size)
	{
		out.write(data, size);
		return true;
	}

	static BinaryFileWriter* Create()
	{
		return new BinaryFileWriter();
	}
protected:
	BinaryFileWriter() {};
	~BinaryFileWriter() {}
};
#endif